<template>
<!-- pages/user_address/user_address.wxml -->
<view class="user-address">
     
        <view class="address-list" >
          
                <view v-for="(item, index) in addressList" :key="index" class="item bg-white mb20" :data-id="item.id" @tap="onSelect">
                    <view class="address">
						<view class="flex-row-flex-start">
							<view v-if='item.is_default' class="label flex-column-center">
								默认
							</view>
							<view class="consignee md bold">
							    {{item.contact}}
							    <text class="phone ml10">{{item.telephone}}</text>
							</view>
							
						</view>
						<view class="lighter sm mt10">
						    {{item.province}} {{item.city}} {{item.district}} {{item.address}}
						</view>
						
                    </view>
                    <view class="operation flex-row-flex-end">
                       
                        
                        <view class="flex-row-flex-end">
                            <view class="row mr20" @click.stop="editAddress(item.id)">
                                <!-- <image class="icon-md mr10" src="/static/images/icon_edit.png"></image> -->
                                编辑
                            </view>
                            <view class="row ml20" :data-id="item.id" @tap.stop="showSurePop">
                                <!-- <image class="icon-md mr10" src="/static/images/icon_del_1.png"></image> -->
                                删除
                            </view>
                        </view>
                    </view>
                </view>
       
        </view>
        <u-modal 
          id="delete-dialog"
          v-model="deleteSure"
          :showCancelButton="true"
          confirm-text="确认"
          confirm-color="#FF4D40"
          :show-title="false"
          @confirm="delAddressFun" 
          @cancel="hidePop"
        >
            <view class="column-center tips-dialog">
                <!-- <image class="icon-lg" src="/static/images/icon_warning.png"></image> -->
                <u-icon name="error-circle-fill" color="#FF4D40" size="56" />
                <view style="margin-top:30rpx">确认删除该地址吗？</view>
            </view>
        </u-modal>
				<loading-footer v-if="!addressList.length" :status="status" slotEmpty>
					<view class="data-null column-center" slot="empty">
						<image src="/static/images/profit_null.png" class="img-null" />
						<text class="sm muted">暂无地址～</text>
					</view>
				</loading-footer>
    <view class="footer fixed-bottom">
		<view @click="addAddress()" class="addButton flex-column-center">
			添加收货地址
		</view>
    </view>
</view>
</template>

<script>

import { getAddressLists, delAddress, setDefaultAddress } from '@/api/user';
import wechath5 from '@/utils/wechath5'
import {isWeixinClient} from '@/utils/tools'
export default {
  data() {
    return {
		status: 'empty',
      addressList: [],
      hasAddress: true,
      deleteSure: false,
      currentId: 0,
	  isWeixin: true
    };
  },
  
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.type = options.type;
	//#ifdef H5
	this.isWeixin = isWeixinClient()
	//#endif
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAddressListsFun();
  },

  methods: {
    onSelect(e) {
      if (this.type) {
        const dataset = e.currentTarget.dataset;
        const id = dataset.id;
        const address = this.addressList.find(item => item.id == id) || {};
        
        uni.$emit('selectaddress', {
          id,
          address
        });
        
        uni.navigateBack();
      }
    },

    addAddress() {
      uni.navigateTo({
        url: '/pages/address_edit/address_edit'
      });
    },

    editAddress(id) {
      uni.navigateTo({
        url: `/pages/address_edit/address_edit?id=${id}`
      });
    },

    getAddressListsFun() {
      getAddressLists().then(res => {
        if (res.code == 1) {
          if (res.data.length) {
			  this.addressList = res.data;
			  this.hasAddress = true;
          } else {
			    this.addressList = [];
			  this.hasAddress = false;
          }
        } else {
            this.hasAddress = false
        }
      });
    },

    radioChange(e) {
      let id = e.detail.value;
      console.log(e);
      setDefaultAddress(id).then(res => {
        if (res.code == 1) 
        this.getAddressListsFun();
      });
    },

    onLoadFun() {
      this.getAddressListsFun();
    },

    delAddressFun(e) {
      let id = this.currentId;
      delAddress(id).then(res => {
        if (res.code == 1) {
          this.$toast({
            title: res.msg
          });
		  this.deleteSure = false
          this.getAddressListsFun();
        }
      });
    },

    getWxAddressFun() {
		// #ifdef H5
		wechath5.getWxAddress().then((res) => {
			uni.setStorageSync('wxAddress', JSON.stringify(res));
			setTimeout(() => {
			  uni.navigateTo({
			    url: `/pages/address_edit/address_edit`
			  });
			}, 200);
		})
		// #endif
		// #ifdef MP-WEIXIN
      uni.authorize({
        scope: 'scope.address',
        success: function (res) {
          uni.chooseAddress({
            success: function (res) {
              uni.setStorageSync('wxAddress', JSON.stringify(res));
              setTimeout(() => {
                uni.navigateTo({
                  url: `/pages/address_edit/address_edit`
                });
              }, 200);
            },
            fail: function (res) {
              if (res.errMsg == 'chooseAddress:cancel') return this.$toast({
                title: '取消选择'
              });
            }
          });
        },
        fail: function (res) {
          uni.showModal({
            title: '您已拒绝导入微信地址权限',
            content: '是否进入权限管理，调整授权？',

            success(res) {
              if (res.confirm) {
                uni.openSetting({
                  success: function (res) {}
                });
              } else if (res.cancel) {
                return this.$toast({
                  title: '已取消！'
                });
              }
            }

          });
        }
      });
	  // #endif
    },
	
    showSurePop: function (e) {
      this.deleteSure = true;
      this.currentId = e.currentTarget.dataset.id;
    },
    hidePop: function (e) {
      this.deleteSure = false;
    }
  }
};
</script>
<style lang="scss">
/* pages/user_address/user_address.wxss */
.user-address {
    padding-bottom: calc(140rpx + env(safe-area-inset-bottom));
	.no-address {
		padding-top: 300rpx;
		text-align: center;
	}
	.address-list {
		
		.item {
			margin-left: 5%;
			width: 90%;
			margin-top: 20rpx;
			// margin: 20rpx;
			background: #FFFFFF;
			border-radius: 11rpx 11rpx 11rpx 11rpx;
			opacity: 1;
		    padding: 10rpx 30rpx;
			height: 220rpx;
			.address {
			    padding: 20rpx 0;
			    border-bottom: $-solid-border;
			}
			.operation {
			    height: 80rpx;
			}
		}
	}
	.footer {
	   
		padding-bottom: env(safe-area-inset-bottom);
		
	}
}

.tips-dialog {
    height: 230rpx;
    width: 100%;
}
.label{
	margin-right: 20rpx;
	font-size: 23rpx;
	font-family: PingFang SC-Regular, PingFang SC;
	font-weight: 400;
	color: #FFFFFF;
	width: 76rpx;
	height: 40rpx;
	background: #FF4D40;
	border-radius: 4rpx 4rpx 4rpx 4rpx;
	opacity: 1;
}
.addButton{
	font-size: 27rpx;
	font-family: PingFang SC-Medium, PingFang SC;
	font-weight: 500;
	color: #FFFFFF;
	margin: 28rpx;
	height: 86rpx;
	background: #FF4D40;
	border-radius: 11rpx 11rpx 11rpx 11rpx;
	opacity: 1;
}
</style>